package com.example.mybatisplusdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.mapper.RatingMapper;
import com.example.mybatisplusdemo.model.domain.Store;
import com.example.mybatisplusdemo.mapper.StoreMapper;
import com.example.mybatisplusdemo.model.dto.LocationDTO;
import com.example.mybatisplusdemo.service.IStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2025-07-04
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private RatingMapper ratingMapper;

    @Override
    public List<Store> getStoresByCategory(String category) {
        return storeMapper.getStoresByCategory(category);
    }

    @Override
    public List<Store> getTop20FavoriteStores() {
        return storeMapper.selectTop20FavoriteStores();
    }

    @Override
    public Long getFavoriteCountById(Long id) {
        return storeMapper.getFavoriteCountById(id);
    }

    @Override
    public Long getReviewCountById(Long id) {
        return storeMapper.getReviewCountById(id);
    }

    @Override
    public Double getAvgScoreById(Long id) {
        return storeMapper.getAvgScoreById(id);
    }

    @Override
    public Store getStoreById(Long id) {
        return storeMapper.selectStoreById(id);
    }

    @Override
    public boolean updateStore(Store store) {
        storeMapper.updateById(store);
        return true;
    }

    @Override
    public List<Store> getStoreListByMerchantId(Long merchantId) {
        return storeMapper.getStoreListByMerchantId(merchantId);
    }

    @Override
    public List<Store> getTop20FiveStarStores() {
        return storeMapper.selectTop20FiveStarStores();
    }

    @Override
    public List<Store> getTop20ReviewStores() {
        return storeMapper.selectTop20ReviewStores();
    }

    @Override
    public List<Store> getTop10AvgScoreStores() {
        return storeMapper.selectTop10AvgScoreStores();
    }

    @Override
    public Page<Store> getPendingStores(Page<Store> page) {
        return storeMapper.getPendingStores(page);
    }

    @Override
    public Long getFiveStarCountById(Long id) {
        return storeMapper.getFiveStarCountById(id);
    }

    @Override
    public double getStoreRating(Long storeId) {
        double rating = ratingMapper.selectById(storeId).getScoreTotal();
        return rating;
    }

    @Override
    public List<Store> searchStores(String keyword) {
        // 用QueryWrapper实现模糊搜索
        QueryWrapper<Store> wrapper = new QueryWrapper<>();
        wrapper.like("name", keyword) //称模糊匹配
                .or().like("description", keyword) //描述模糊匹配
                .eq("is_deleted", false) //排除软删除数据
                .eq("status", 1); //假设1为正常状态
        return list(wrapper);
    }

    @Override
    public boolean approveStore(Long storeId) {
        return storeMapper.approveStore(storeId);
    }

    @Override
    public LocationDTO getStoreLocationById(Long storeId) {
        return storeMapper.getStoreLocationById(storeId);
    }

    @Override
    public Double calculateDistance(Double lon1, Double lat1, Double lon2, Double lat2) {
        return storeMapper.calculateDistance(lon1, lat1, lon2, lat2);
    }

    @Override
    public boolean closeStoreById(Long id) {
        boolean b = storeMapper.closeStoreById(id);
        return b;
    }

    @Override
    public boolean rejectStore(Long storeId) {
        return storeMapper.rejectStore(storeId);
    }


}
